package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> findPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable){
        return ResponseEntity.ok(this.goodsService.findPage(page,rows,key,saleable));
    }
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.saveGoods(spuDTO);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> queryGoodsById(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.goodsService.queryGoodsById(id));
    }
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuById(@RequestParam("id") Long id){
    return ResponseEntity.ok(this.goodsService.querySkuById(id));
    }
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSaleableById(@RequestParam("id") Long id,@RequestParam("saleable") Boolean sale){
        this.goodsService.updateSaleableById(id,sale);
        return ResponseEntity.ok().build();
    }
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.updateGoods(spuDTO);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    @DeleteMapping("goods")
    public ResponseEntity<Void> deleteGoodsById(@RequestParam("id") Long id){
        this.goodsService.deleteGoodsById(id);
        return ResponseEntity.ok().build();
    }
}
